package com.ftg.product.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftg.product.entity.MonitorRule;
import com.ftg.product.entity.Organization;
import com.ftg.product.service.IOrganizationService;
import com.ftg.product.service.ImonitorRule;
import com.ftg.product.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 智能控制 -  规则监控  -  规则管理
 *
 * @author kangjx
 * @createTime 2022/8/23
 */
@Api(tags = "规则管理")
@RequestMapping("/ruleManager")
@RestController
//@CrossOrigin(origins = {"http://www.baidu.com"},methods = {RequestMethod.GET,RequestMethod.POST,RequestMethod.PUT})
public class RuleController {

    @Resource
    ImonitorRule imonitorRule;

    @Resource
    IOrganizationService iOrganizationService;

    /**
     * 规则管理 下面的列表
     * 对应老系统里面的
     * znjk/gzjk/rule/rulesmgmt?orgId=127634&choiceSearch=hide
     */
    @GetMapping("/ruleList1")
    @ApiOperation(value = "规则管理列表 - RuleController 智能控制- 规则监控 - 规则管理 /ruleManager")
    public JsonToWeb showMonitorRuleList1(
            @ApiParam(value = "当前页码", defaultValue = "1", name = "pageNo") @RequestParam(value = "pageNo", defaultValue = "1") long pageNo,
            @ApiParam(value = "显示条目", defaultValue = "20", name = "pageNum", required = false) @RequestParam(value = "pageNum", defaultValue = "20", required = false) long pageNum,
            @ApiParam(value = "组织编码", name = "orgId") @RequestParam(value = "orgId") BigDecimal orgId,
            @ApiParam(value = "token", name = "token") @RequestHeader(value = "token") String token
    ) {
        TblStaffUtil tblStaffUtil = null;
        try {
            tblStaffUtil = DealUserToken.parseUserToken(token);
            if (Objects.isNull(tblStaffUtil)) {
                return JsonToWeb.checkToken();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        IPage<MonitorRule> ipage = new Page<>(pageNo, pageNum);
        return JsonToWeb.success(imonitorRule.page(ipage));
    }

    @GetMapping("/layuirulepage")
    @ApiOperation(value = "layui分页测试功能")
    public JSONObject showMonitorRuleList2(
            @ApiParam(value = "当前页码", defaultValue = "1", name = "page") @RequestParam(value = "page", defaultValue = "1") long page,
            @ApiParam(value = "显示条目", defaultValue = "20", name = "limit") @RequestParam(value = "limit", defaultValue = "20", required = false) long limit,
            @ApiParam(value = "组织编码", name = "orgId") @RequestParam(value = "orgId") BigDecimal orgId,
            @ApiParam(value = "token", name = "token") @RequestHeader(value = "token") String token
    ) {
        TblStaffUtil tblStaffUtil = null;
        try {
            tblStaffUtil = DealUserToken.parseUserToken(token);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //为了满足前台layui的要求我们修改的对应的数据返回格式
        IPage<MonitorRule> ipage = new Page<>(page, limit);
        //这个原是后台的分页方法
        imonitorRule.page(ipage);
        JSONObject json = new JSONObject();
        json.put("code",0);
        json.put("msg","操作成功");
        json.put("data", ipage.getRecords());
        json.put("count",ipage.getTotal());
        return json;
    }

    @ApiOperation(value = "行业规则库---》左侧菜单",tags = "行业规则库")
    @GetMapping(value = "/gzjk/rule/hy_left")
    public JsonToWeb hy_left(
            @ApiParam(value = "类型", name = "type", required = false) @RequestParam(value = "type") String type,
            @ApiParam(value = "组织编码", name = "orgid") @RequestParam(value = "orgid") String orgid,
            @ApiParam(value = "token", name = "token") @RequestHeader(value = "token") String token) {





        TblStaffUtil tblStaffUtil = null;
        try {
            tblStaffUtil = DealUserToken.parseUserToken(token);
            if (Objects.isNull(tblStaffUtil)) {
                return JsonToWeb.checkToken();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        TblOrganizationUtil attribute = tblStaffUtil.getCurrentOrg();//选则的机构
        TblOrganizationUtil attribute1 =  tblStaffUtil.getLinkOrg();//当前用户的机构
        TblLoginTypeUtil tblStaff = tblStaffUtil.getLoginType();
//
//        tblStaffUtil.getLinkOrg()  //用户 所在的组织
//        tblStaffUtil.getCurrentOrg() // 选中组织信息
//        tblStaffUtil.getLoginType() // 用户信息

        List<Organization> findOrgTree = iOrganizationService.findOrgTreeObjByHY(orgid);
        String tree = "";
        String treeName="";
        if (StringUtils.isNotEmpty(orgid) && !orgid.equals(findOrgTree.get(0).getOrgid().toString())) {
            iOrganizationService.initOrgByOrgtype(Organization.TYPE_HY_GZK, "行业规则库", orgid);
            tree = iOrganizationService.findOrgByorgIdHyWt(orgid);
            Organization o=iOrganizationService.getById(orgid);
            treeName=o.getOrgname();
        }else {
            treeName=findOrgTree.get(0).getOrgname();
        }
        int startNum = tree.indexOf("-1_");
        String defaultId = startNum > 0 ? tree.substring(startNum).substring(3, tree.substring(startNum).indexOf("'")): "";
        Map<String,Object> mv = new HashMap<>();
        mv.put("icode", orgid);
        mv.put("treeName", treeName.replace("┣", ""));
        mv.put("orgTree", findOrgTree);
        mv.put("defaultId", defaultId);
        mv.put("selectorgtype", Organization.TYPE_HY_GZK);
        mv.put("targetFrame", "mainFramex");
        mv.put("type", type);
        return JsonToWeb.success(mv);
    }

}
